﻿using MetaProperties.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using MetaProperties.Framework;

namespace MetaProperties.Core.Tests
{
    using Rhino.Mocks;


    /// <summary>
    ///This is a test class for IsolatingEventFilterTest and is intended
    ///to contain all IsolatingEventFilterTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IsolatingEventFilterTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /////// <summary>
        ///////A test for MetaPropertyConfiguration
        ///////</summary>
        ////[TestMethod()]
        ////[DeploymentItem("MetaProperties.Core.dll")]
        ////public void MetaPropertyConfigurationTest()
        ////{
        ////    IsolatingEventFilter ief = new IsolatingEventFilter();
        ////    PrivateObject po = new PrivateObject(ief);
        ////    IsolatingEventFilter_Accessor target = new IsolatingEventFilter_Accessor(po);

        ////    MetaPropertyConfiguration expected = new MetaPropertyConfiguration(null, null);
        ////    MetaPropertyConfiguration actual;
        ////    target.MetaPropertyConfiguration = expected;
        ////    actual = target.MetaPropertyConfiguration;

        ////    Assert.AreEqual(expected, actual);
        ////}

        /// <summary>
        ///A test for IsActive
        ///</summary>
        [TestMethod()]
        public void IsActiveTest()
        {
            // NB: This can't really be independently tested as it is static.
            Assert.IsFalse(IsolatingEventFilter.IsActive);

            IsolatingEventFilter.UnhandledEventHandlerException += this.IsolatingEventFilter_EventHandlerException;

            bool isActive = IsolatingEventFilter.IsActive;

            IsolatingEventFilter.UnhandledEventHandlerException -= this.IsolatingEventFilter_EventHandlerException;

            Assert.IsTrue(isActive);
        }

        void IsolatingEventFilter_EventHandlerException(object sender, Exception e)
        {
        }

        /////// <summary>
        ///////A test for SetMetaPropertyConfiguration
        ///////</summary>
        ////[TestMethod()]
        ////public void SetMetaPropertyConfigurationTest()
        ////{
        ////    IsolatingEventFilter target = new IsolatingEventFilter();
        ////    PrivateObject po = new PrivateObject(target);
        ////    IsolatingEventFilter_Accessor accessor = new IsolatingEventFilter_Accessor(po);

        ////    MetaPropertyConfiguration configuration = new MetaPropertyConfiguration(null, null);
        ////    target.SetMetaPropertyConfiguration(configuration);

        ////    Assert.AreEqual(configuration, accessor.MetaPropertyConfiguration);
        ////}

        ////bool performFilterTest_isHandlerCalled = false;

        /////// <summary>
        ///////A test for PerformFilter
        ///////</summary>
        ////[TestMethod()]
        ////public void PerformFilterTest()
        ////{
        ////    MockRepository mocks = new MockRepository();

        ////    IFilterPipeline pipeline1 = mocks.DynamicMock<IFilterPipeline>();

        ////    object sender = this;
        ////    EventArgs args = EventArgs.Empty;

        ////    Expect.Call(() => pipeline1.Resume(sender, args)).Repeat.Once();

        ////    mocks.ReplayAll();

        ////    IsolatingEventFilter target = new IsolatingEventFilter();
        ////    target.PerformFilter(sender, args, pipeline1);

        ////    mocks.VerifyAll();
        ////}


        /////// <summary>
        ///////A test for PerformFilter
        ///////</summary>
        ////[TestMethod()]
        ////public void PerformFilterTest_ExceptionHandled()
        ////{
        ////    MockRepository mocks = new MockRepository();

        ////    IFilterPipeline pipeline = mocks.DynamicMock<IFilterPipeline>();

        ////    object sender = this;
        ////    EventArgs args = EventArgs.Empty;

        ////    Expect.Call(() => pipeline.Resume(sender, args))
        ////        .Do((Action<object, EventArgs>)delegate { throw new InvalidOperationException(); })
        ////        .Repeat.Once();

        ////    mocks.ReplayAll();

        ////    IsolatingEventFilter target = new IsolatingEventFilter();

        ////    Assert.IsFalse(this.performFilterTest_isHandlerCalled);

        ////    IsolatingEventFilter.UnhandledEventHandlerException += this.IsolatingEventFilter_EventHandlerException_PerformFilterTest;
        ////    target.PerformFilter(sender, args, pipeline);
        ////    IsolatingEventFilter.UnhandledEventHandlerException -= this.IsolatingEventFilter_EventHandlerException_PerformFilterTest;

        ////    Assert.IsTrue(this.performFilterTest_isHandlerCalled);

        ////    mocks.VerifyAll();
        ////}

        ////void IsolatingEventFilter_EventHandlerException_PerformFilterTest(object sender, Exception e)
        ////{
        ////    Assert.IsNull(sender);
        ////    performFilterTest_isHandlerCalled = true;
        ////}


        /////// <summary>
        ///////A test for PerformFilter
        ///////</summary>
        ////[TestMethod()]
        ////public void PerformFilterTest_ExceptionUnhandled()
        ////{
        ////    MockRepository mocks = new MockRepository();

        ////    IFilterPipeline pipeline = mocks.DynamicMock<IFilterPipeline>();

        ////    object sender = this;
        ////    EventArgs args = EventArgs.Empty;

        ////    Expect.Call(() => pipeline.Resume(sender, args))
        ////        .Do((Action<object, EventArgs>)delegate { throw new InvalidOperationException("TEST"); })
        ////        .Repeat.Once();

        ////    mocks.ReplayAll();

        ////    IsolatingEventFilter target = new IsolatingEventFilter();

        ////    Assert.IsFalse(this.performFilterTest_isHandlerCalled);

        ////    bool exceptionThrown = false;
        ////    try
        ////    {
        ////        target.PerformFilter(sender, args, pipeline);
        ////    }
        ////    catch (Exception t)
        ////    {
        ////        exceptionThrown = true;
        ////        Assert.AreEqual("TEST", t.Message);
        ////    }

        ////    Assert.IsTrue(exceptionThrown);

        ////    Assert.IsFalse(this.performFilterTest_isHandlerCalled);

        ////    mocks.VerifyAll();
        ////}


        bool handleExceptionTest_isHandlerCalled = false;

        /// <summary>
        ///A test for HandleException
        ///</summary>
        [TestMethod()]
        public void HandleExceptionTest()
        {
            Exception t = new Exception();

            Assert.IsFalse(IsolatingEventFilter.HandleException(t));

            IsolatingEventFilter.UnhandledEventHandlerException += this.IsolatingEventFilter_EventHandlerException_HandleExceptionTest;

            Assert.IsTrue(IsolatingEventFilter.HandleException(t));

            Assert.IsTrue(this.handleExceptionTest_isHandlerCalled);

            IsolatingEventFilter.UnhandledEventHandlerException -= this.IsolatingEventFilter_EventHandlerException_HandleExceptionTest;
        }

        void IsolatingEventFilter_EventHandlerException_HandleExceptionTest(object sender, Exception e)
        {
            handleExceptionTest_isHandlerCalled = true;
        }

        /////// <summary>
        ///////A test for IsolatingEventFilter Constructor
        ///////</summary>
        ////[TestMethod()]
        ////public void IsolatingEventFilterConstructorTest()
        ////{
        ////    IsolatingEventFilter target = new IsolatingEventFilter();
        ////    PrivateObject po = new PrivateObject(target);
        ////    IsolatingEventFilter_Accessor accessor = new IsolatingEventFilter_Accessor(po);

        ////    Assert.IsNull(accessor.MetaPropertyConfiguration);
        ////}
    }
}
